﻿using _10_HPBar.CharacterBase;
using _10_HPBar.GameFrame.Interface;
using _10_HPBar.Items;
using _10_HPBar.Items.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace _10_HPBar.GameFrame.System
{
    internal class InventorySystem
    {
        List<Item> items;
        int x, y, width, height;
        int selection, maxShow;
        int startIndex, endIndex;
        Character owner;

        public InventorySystem(int x, int y, int width, int height,Character owner)
        {
            items = new List<Item>();
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.owner = owner;
            selection = 0;
            maxShow = height - 2;
            startIndex = 0;
            endIndex = 0;
        }

        public void Add(Item item)
        {
            items.Add(item);
            //endIndex++;
            //if (endIndex > maxShow - 1)
            //    endIndex = maxShow - 1;
            MoveShowIndex(true);
        }

        public void Remove(Item item)
        {
            items.Remove(item);
            MoveShowIndex(false);
        }

        public void Input()
        {
            if (Game.input.IsKeyPressed(ConsoleKey.W))
            {
                UpRoll();
            }
            if (Game.input.IsKeyPressed(ConsoleKey.S))
            {
                DownRoll();
            }
            if (Game.input.IsKeyPressed(ConsoleKey.Enter))
            {
                if(startIndex + selection < items.Count)
                {
                    Use(items[startIndex + selection]);
                }
            }
        }

        public void Render()
        {
            Utility.DrawFrame(x, y, width, height,ConsoleColor.White);

            if(items.Count > 0)
            {
                for (int i = startIndex, j = 0; i <= endIndex; i++, j++)
                {
                    Utility.MyPrint(items[i].Name, x + 2, y + 1 + j);
                }
            }

            Utility.MyPrint(">>", x + 1, y + 1 + selection);
            Utility.MyPrint("<<", x + width-2, y + 1 + selection);
        }

        public void Use(Item item, IAttack? target = null)
        {
            // 反射 获取类的具体类型
            // 如果是材料 直接删除
            // 如果是消耗品 就使用后删除
            // 如果是装备 就装备到角色的装备栏
            Type? type = item.GetType().BaseType;
            if(type == typeof(Equipment))
            {
                ((Equipment)item).Equip(owner);
            }
            else if (type == typeof(Consumable))
            {
                ((Consumable)item).Use(target);
            }
            else if (type == typeof(Material))
            {
                
            }
        }

        void UpRoll()
        {
            selection--;
            if (selection < 0)
            {
                selection = 0;
                // 将道具列表往下移一行
                if(items.Count > maxShow)
                {
                    MoveShowIndex(true);
                }
            }
        }

        void DownRoll()
        {
            if(selection < items.Count-1)
               selection++;

            if(selection > maxShow - 1)
            {
                selection = maxShow - 1;
                // 将道具列表往上移一行
                if (items.Count > maxShow)
                {
                    MoveShowIndex(false);
                }
            }
        }

        void MoveShowIndex(bool up)
        {
            int offset = 0;
            if(items.Count > maxShow)
            {
                offset = maxShow - 1;
            }
            else
            {
                offset = items.Count-1;
            }

            if (up)
            {
                startIndex--;
                if (startIndex < 0)
                {
                    startIndex = 0;
                }
                endIndex = startIndex + offset;
            }
            else
            {
                endIndex++;
                if(endIndex > items.Count - 1)
                {
                    endIndex = items.Count - 1;
                }
                startIndex = endIndex - offset;
            }
        }
    }
}
